﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RN.ERP.Entity;

namespace RN.ERP.DAL
{
    public class DictionaryDAL
    {
        QD_Context dbContext = new QD_Context();

        public IList<KeyValue> GetCity(string condition)
        {
            string sql = "SELECT [Title] [value],[Code] [key] FROM [dbo].[a_AreaInfo] " +
                        (string.IsNullOrEmpty(condition) ? string.Empty : " where 1=1 " + condition) + " order by sortIndex asc";
            return dbContext.Database.SqlQuery<KeyValue>(sql).ToList();
        }

        public IList<KeyValue> GetDistrict(string condition)
        {
            string sql = "SELECT [Title] [value],[SortCode] [key] FROM [dbo].[a_AreaSubInfo] " +
                        (string.IsNullOrEmpty(condition) ? string.Empty : " where 1=1 " + condition) + " order by [SortCode] asc";
            return dbContext.Database.SqlQuery<KeyValue>(sql).ToList();
        }

        public IList<KeyValue> GetAreaSSQ(string condition)
        {
            string sql = "SELECT [Title] [value],[Code] [key] FROM [dbo].[a_AreaSSQ] " +
                        (string.IsNullOrEmpty(condition) ? string.Empty : " where 1=1 " + condition) + " order by [id] asc";
            return dbContext.Database.SqlQuery<KeyValue>(sql).ToList();
        }

        public IList<KeyValue> GetCorpType(string condition)
        {
            return dbContext.Dictionarys.Where(m => m.ParentSID == "CorpType").OrderBy(m => m.Code).Select(m => new KeyValue { Key = m.Code, Value = m.Name }).ToList();
        }

        public IList<KeyValue> GetEmpDegreeType(string condition)
        {
            string sql = "SELECT [Name] [value],[Code] [key] FROM [dbo].[T_Sys_Dictionary] where parentSID='EmpDegree' " +
                        condition + " order by [Code] asc";
            return dbContext.Database.SqlQuery<KeyValue>(sql).ToList();
        }

        public IList<KeyValue> GetDegreeType(string condition)
        {
            string sql = "SELECT [Name] [value],[Code] [key] FROM [dbo].[T_Sys_Dictionary] where parentSID='Degree' " +
                        condition + " order by [Code] asc";
            return dbContext.Database.SqlQuery<KeyValue>(sql).ToList();
        }

        public IList<KeyValue> GetDic(string condition)
        {
            var result = from dic in dbContext.Dictionarys
                         where dic.ParentSID == condition && dic.IsDel != true
                         orderby dic.SortValue ascending
                         select new KeyValue()
                         {
                             Key = dic.Code,
                             Value = dic.Name
                         };
            return result.ToList<KeyValue>();
        }
    }
}
